From 73fbb411d147dc34892cc59bc729faa67baeb6ef Mon Sep 17 00:00:00 2001
From: Anton Batenev <antonbatenev@yandex.ru>
Date: Thu, 7 Jan 2016 22:32:34 +0300
Subject: [PATCH] Fix application crash with variable length frame size
 webcams.

For example with pac7302 cam driver and PJPG pixel format, frames
has variable length and v4lconvert_convert crash due incorrect
source size specified (sizeimage is the maximum number of bytes
required to hold an image for variable length compressed data).

Also if v4lconvert_convert will fail we must return correct value
from v4l_getframe - frame data is not valid and can not be used.
---
 src/xlib/v4l.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/src/xlib/v4l.c b/src/xlib/v4l.c
index 9cbb126..8b4fec0 100644
--- a/src/xlib/v4l.c
+++ b/src/xlib/v4l.c
@@ -312,7 +312,10 @@ int v4l_getframe(uint8_t *y, uint8_t *u, uint8_t *v, uint16_t width, uint16_t he
 
     /* assumes planes are continuous memory */
 #ifndef NO_V4LCONVERT
-    v4lconvert_convert(v4lconvert_data, &fmt, &dest_fmt, data, fmt.fmt.pix.sizeimage, y, (video_width * video_height * 3) / 2);
+    int result = v4lconvert_convert(v4lconvert_data, &fmt, &dest_fmt, data, buf.bytesused, y, (video_width * video_height * 3) / 2);
+    if (result == -1) {
+        debug("v4lconvert_convert error %s\n", v4lconvert_get_error_message(v4lconvert_data));
+    }
 #else
     if(fmt.fmt.pix.pixelformat == V4L2_PIX_FMT_YUYV) {
         yuv422to420(y, u, v, data, video_width, video_height);
@@ -325,5 +328,9 @@ int v4l_getframe(uint8_t *y, uint8_t *u, uint8_t *v, uint16_t width, uint16_t he
         debug("VIDIOC_QBUF error %d, %s\n", errno, strerror(errno));
     }
 
+#ifndef NO_V4LCONVERT
+    return (result == -1 ? 0 : 1);
+#else
     return 1;
+#endif
 }
